package com.shuqi.activity.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.application.ShuqiApplication;
import com.shuqi.common.Constant;
import com.shuqi.common.PayTheme;
import com.shuqi.common.utils.Log4an;
import com.shuqi.controller.R;
import com.shuqi.database.model.BookCataLog;
import com.shuqi.database.model.BookInfo;

/**
 * 收费书籍目录adapter
 * 
 * @author yhw
 */
public class PayCatalogAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<BookCataLog> mBookCataLoglist;

    private int mTextFontColor;
    private int mUnTextFontColor;
    private int mBuyDrawable;
    private int mMarkDrawable;
    private String mSelectorChapterId;
    private int mSelectorTextFontColor;

    public PayCatalogAdapter(Context context, List<BookCataLog> list) {
        this.inflater = LayoutInflater.from(context);
        initData(list);
        // 主题相关
        setTheme();
    }

    private void initData(List<BookCataLog> list) {
        if (list != null) {
            List<BookCataLog> listBuffer = new ArrayList<BookCataLog>();;
            String name = "";
            for (BookCataLog bookCatalog : list) {
                if (bookCatalog.getChapterState() == BookCataLog.UN_IS_CHATPTER) {
                    name = bookCatalog.getChapterName();
                } else {
                    if (!TextUtils.isEmpty(name)) {
                        if(!bookCatalog.getChapterName().startsWith(name)){
                            bookCatalog.setChapterName(name + " " + bookCatalog.getChapterName());
                        }
                    }
                    listBuffer.add(bookCatalog);
                    name = "";
                }
            }
            mBookCataLoglist = listBuffer;
        }
    }

    /**
     * 更新数据
     * 
     * @param mCatalog 章节数据
     */
    public void setList(List<BookCataLog> list) {
        initData(list);
    }

    public int getPosition(String chapterId) {
        int mSelectorPosition = 0;
        if (chapterId != null) {
            if (mBookCataLoglist != null) {
                for (BookCataLog catalog : mBookCataLoglist) {
                    if (catalog.getChapterId().equals(chapterId)) {
                        break;
                    }
                    ++mSelectorPosition;
                }
            }
        }
        return mSelectorPosition;
    }

    public void setTheme() {
        int themeIndex = PayTheme.getCurrentTheme(ShuqiApplication.getContext());
        mTextFontColor = Constant.READ_FONT_COLORS[themeIndex];
        mUnTextFontColor = Constant.CATLOG_NO_COLORS[themeIndex];
        mBuyDrawable = Constant.CATLOG_BUY_ICON[themeIndex];
        if (PayTheme.PAY_THEME == themeIndex) {// 夜间
            mSelectorTextFontColor = 0xff0c6b47;
            mMarkDrawable = Constant.CATALOG_BOOKMARK_IMAGE[0];
        } else {
            mSelectorTextFontColor = 0xff14be7d;
            mMarkDrawable = Constant.CATALOG_BOOKMARK_IMAGE[1];
        }
    }

    private static class Holder {
        public TextView tv1;
        public ImageView img1;
    }


    @Override
    public boolean hasStableIds() {
        return true;
    }

    public BookCataLog getInfoFromCataLog(int groupPosition, int childPosition) {
        if (mBookCataLoglist != null) {
            BookCataLog catalog = mBookCataLoglist.get(childPosition);
            return catalog;
        }
        return null;
    }

    public void setSelectorChapterId(String chapterId) {
        mSelectorChapterId = chapterId;
    }

    /***
     * 切换制定章节id为免费
     * 
     * @param chapterId
     * @param payMode
     */
    public void resetChapterPayState2NoPay(String chapterId) {
        resetChapterPayState(chapterId, BookCataLog.HAS_PAY);
    }

    public void resetChapterPayState(String chapterId, int payState) {

        if (mBookCataLoglist == null || TextUtils.isEmpty(chapterId)) {
            return;
        }
        if (mBookCataLoglist != null) {
            final int cSize = mBookCataLoglist.size();
            for (int childPosition = 0; childPosition < cSize; childPosition++) {
                BookCataLog catalog = mBookCataLoglist.get(childPosition);
                if (chapterId.equals(catalog.getChapterId())) {
                    catalog.setPayState(payState);
                    break;
                }
            }
        }
    }

    @Override
    public int getCount() {
        if (mBookCataLoglist != null) {
            return mBookCataLoglist.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mBookCataLoglist != null) {
            return mBookCataLoglist.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = inflater.inflate(R.layout.bookcontent_chapter_list_item, parent, false);
        }
        Holder holder = (Holder) view.getTag();
        if (holder == null) {
            holder = new Holder();
            holder.tv1 = (TextView) view.findViewById(R.id.textView1);
            holder.img1 = (ImageView) view.findViewById(R.id.imgView_itemlayout_listview3);
            view.setTag(holder);
        }
        holder.tv1.setText(mBookCataLoglist.get(position).getChapterName());
        
        if (mSelectorChapterId != null
                && mBookCataLoglist.get(position).getChapterId().equals(mSelectorChapterId)) {
            holder.img1.setVisibility(View.VISIBLE);
            holder.img1.setImageResource(mMarkDrawable);
            holder.tv1.setTextColor(mSelectorTextFontColor);
            if(PayTheme.getCurrentTheme() == 3){
                holder.tv1.setTextColor(0xff1ae195);
            }
        } else {
            int payMode = mBookCataLoglist.get(position).getPayMode();
            if (payMode == BookInfo.ALL_BOOK || payMode == BookInfo.ALL_CHAPTER) {
                if (mBookCataLoglist.get(position).getPayState() == BookCataLog.NO_PAY) {
                    holder.img1.setVisibility(View.VISIBLE);
                    holder.img1.setImageResource(mBuyDrawable);
                } else {
                    holder.img1.setVisibility(View.GONE);
                }
            } else {
                holder.img1.setVisibility(View.GONE);
            }
            
            if(mBookCataLoglist.get(position).getDownloadState() == BookCataLog.UN_DOWN){
                holder.tv1.setTextColor(mUnTextFontColor);
            }else{
                holder.tv1.setTextColor(mTextFontColor);
                holder.img1.setVisibility(View.GONE);
            }
        }
        return view;
    }
}
